<!--
 * @Author: cl
 * @Description: 分页选择器
 * @Date: 2022-11-01 09:01:19
 * @LastEditTime: 2022-11-01 15:01:13
 * @FilePath: \vue3\src\components\Pagination.vue
-->
<template>
    <div :class="{ hidden: hidden }" class="pagination-container">
        <el-pagination :background="props.background" :current-page="currentPage" :page-size="pageSize" :layout="layout"
            :page-sizes="pageSizes" :total="total" v-bind="$attrs" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" :page-count="props.pageCount" />
    </div>
</template>
  
<script setup>
import merge from "lodash/merge";
import { computed } from "vue-demi";

const props = defineProps({
    total: {
        required: true,
        type: Number
    }, page: {
        type: Number,
        default: 1
    },
    limit: {
        type: Number,
        default: 3
    },
    pageSizes: {
        type: Array,
        default() {
            return [10, 20, 30, 40, 50, 100];
        }
    },
    layout: {
        type: String,
        default: "total, sizes, prev, pager, next, jumper"
    },
    background: {
        type: Boolean,
        default: true
    },
    autoScroll: {
        type: Boolean,
        default: true
    },
    hidden: {
        type: Boolean,
        default: false
    },
    params: {
        //请求时传递的其余参数
        type: Object,
        default() {
            return {};
        }
    },
    pageCount: {
        type: Number,
        default: 10
    }
});

const emit = defineEmits()
let currentPage = computed({
    get() {
        return props.page;
    },
    set(val) {
        emit("update:page", val);
    }
})
let pageSize = computed({
    get() {
        return props.limit;
    },
    set(val) {
        emit("update:limit", val);
    }
})
const handleSizeChange = (val) => {
    // console.log('每页' + val +'条')
    /* 切换每页条数的时候，都将pageNo设置回第一页  pageNo: 1*/
    emit("pagination", merge(props.params, { pageNo: 1, pageSize: val }));
    if (props.autoScroll) {
        scrollTo(0, 800);
    }
}
const handleCurrentChange = (val) => {
    // console.log('当前第' + val +'页')
    emit("pagination", merge(props.params, { pageNo: val, pageSize: pageSize.value }));
    if (props.autoScroll) {
        scrollTo(0, 800);
    }
}
</script>
<style scoped>
.pagination-container {
    background: #fff;
    padding: 16px 16px 24px 0;
}

.pagination-container.hidden {
    display: none;
}
</style>
  

